<?php

class CampaignController extends Uuch_BaseController {

    public function joinAction(){
        $this->appendCss('/css/popularize.css');
        $this->appendScript('/js/jquery.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js');
        $this->setTitle('华人网购一站式服务，国际快递低至3-5折');
        $words = array();
        for($i=0;$i<26;$i++){
            $words []= chr(65+$i);
        }
        $this->view->words = $words;
    }
    
    /**
     * 返广链接
     * 两格格式： 1.http://www.uuch.com/campaign/ad/sid/ZXEET
     *          2.http://www.uuch.com/campaign/ad/?ad=12323&uid=P0CCS
     */
    public function adAction(){
        $sid =$this->getSafeParam('sid');
        
        $shareid = $this->getSafeParam('ad');
        $userid = $this->getSafeParam('uid');
        if($sid != NULL && $sid != ''){
            $decryptSid = Application_Model_AdLink::decryptAdUrl($sid);
            $shareid = $decryptSid['id'];
            $userid = $decryptSid['userid'];
        }else{
            $user = Application_Model_User::bystore($userid);
            $userid = $user->Id;
        }
        
        $shareLink = Application_Model_AdLink::load($shareid);
        if($shareLink== null){
            $shareLink = Application_Model_AdLink::getDefault();   
        }
        $url = $shareLink->getTargetUrl($userid);
        setcookie(Uuch_Util::COOKIE_NAME_RECOMMENDER, Uuch_Util::encryptString($userid),time()+3600*24,'/');
        $this->_redirect($url);
    }

    public function couponAction(){
        if(!$this->isLogin()){
            $this->json(array('isSuccess'=> FALSE,'message'=>'您需要登录后才能来领取优惠券。'));
            return;  
        }
        
        //活动已经结合
        return;
        
        $no = $this->getSafeParam('no');
        $batch = Application_Model_CouponBatch::getByNo($no);
        $message=NULL;
        if(!$batch->IsVaild($this->currentUser()->Id,$message)){
            $this->json(array('isSuccess'=>FALSE,'message'=>$message));
            return;
        }
        $coupon = $batch->createCoupon();
        $coupon->UserId = $this->currentUser()->Id;
        $coupon->save();
        $coupon->No = $batch->No.'00'.$coupon->Id;
        $coupon->save();
        $batch->GiveCount++;
        $batch->save();
        $this->json(array('isSuccess'=>TRUE,'message'=>'优惠券领用成功'));
    }
    
    public function daigouAction(){
        $this->appendCss('/css/daigou.css');
        $this->appendScript('/js/jquery.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js');
        $this->setTitle('还在为代购的高额服务费纠结吗？');
    }

    public function testAction(){
        $this->appendScript('/js/jquery.min.js');
    }
    
    public function duanwuAction(){
        $this->appendCss('/duanwu/dw.css');
        $this->setTitle('端午节快乐');
        $this->view->duanwu = Application_Model_AwardHistory::getNewAward(date('Y-m-d H:i:s'));
        $this->appendScript('/js/jquery.min.js');
    }
    
    public function sendawardAction(){
        $this->setTitle('端午节充值送好礼抽奖区');
        $this->appendCss('/huodonghtml/duanwu/dw.css');
        $this->appendScript('/js/jquery.min.js');        
    }
    
    /**
     * 圣诞节活动
     */
    public function christmasAction()
    {
        $this->appendScript('/js/jquery.min.js')
                ->appendScript('/js/jquery.validate.min.js')
                ->appendScript('/js/messages_cn.js');
        $this->setTitle('圣诞狂欢 | 悠奇活动');
        $receiveType = array(
            "10" => false,
            "20" => false,
            "50" => false,
            "100" => false,
        );
        $this->view->startTimes = strtotime("2013-12-12") - time();
        $userId = $this->currentUser()->Id;
        if ($userId){
            foreach ($receiveType as $key => $receiveVal) {
                $countItem = Application_Model_Coupon::search($userId, "FullDiscount".$key);
                if($countItem){
                    $receiveType[$key] = true;
                }
            }
        }
        $this->view->receiveType = $receiveType;
        $this->view->reUrl = urlencode("/campaign/christmas/");
    }
    
    public function receiveAction()
    {
        $activity = $this->getSafeParam("activity");
        $batch = $this->getSafeParam("batch");
        $no = $this->getSafeParam("no");
        if($activity == "christmas"){
            if(!$this->isLogin()){
                $this->json(array('type'=>2,'message'=>'您需要登录后才能来领取代金券。'));
                return;  
            }
            $userId = $this->currentUser()->Id;
            $countItem = Application_Model_Coupon::search($userId, $batch.$no);
            if($countItem){
                $this->json(array("type"=>0,"message"=>"对不起，您已经领取了圣诞节".$no."元的代金券了！"));
                return;
            }
            $batchItem = Application_Model_CouponBatch::getByNo($batch.$no);
            if(!$batchItem || $batchItem->maxCount == $batchItem->giveCount){
                $this->json(array("type"=>0,"message"=>"对不起，圣诞节".$no."元的代金券已经被领取完了！"));
                return;
            }
            $surplusTimes = strtotime($batchItem->dateEnd) - time();
            if($surplusTimes<0){
                $this->json(array("type"=>0,"message"=>"对不起，活动已经结束了！"));
                return;
            }
            if(time() < strtotime($batchItem->dateStart)){
                $this->json(array("type"=>0,"message"=>"活动还未开始，请耐心等候！"));
                return;
            }
            
            $coupon = $batchItem->createCoupon();
            $coupon->UserId = $userId;
            $coupon->save();
            $coupon->No = $batchItem->No.'00'.$coupon->Id;
            $coupon->DateExpired = date("Y-m-d H:i:s", (strtotime($coupon->DateCreated)+(3600*24*31)));
            $coupon->save();
            $batchItem->GiveCount++;
            $batchItem->save();
            $this->json(array("type"=>1,"message"=>"恭喜您，圣诞节".$no."元的代金券领取成功！"));
            return;
        } else {
            $this->json(array("type"=>0,"message"=>"出现未知错误，请刷新再试，如还出现此问题，请联系在线客服！"));
        }
    }
    
    public function mrindexAction()
    {
        $this->appendScript('/js/jquery.min.js')
                ->appendScript('/js/jquery.validate.min.js')
                ->appendScript('/js/messages_cn.js');
//        $this->appendCss('/activity/mrindex/css/style.css');
//        $this->appendCss('/activity/mrindex/css/site.css');
        $this->setTitle('中马专线大惠战');
//        $this->_helper->layout->disableLayout();
    }
    
    public function valentineAction()
    {
//        $this->setTitle('情人节回馈活动');
        $this->_helper->layout->disableLayout();
    }
}

